home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapter8 / crtdlg.c < prev    next >
C/C++ Source or Header  |  1996-01-28  |  7KB  |  226 lines

  1.  
  2. #include <windows.h>  
  3. #include "CrtDlg.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. HWND hDlgModeless = NULL;
  22. BOOL bChecked     = FALSE;
  23. BOOL bRadio1      = TRUE;
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    // Register the main application window class.
  35.    //............................................
  36.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  37.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  38.    wc.cbClsExtra    = 0;                      
  39.    wc.cbWndExtra    = 0;                      
  40.    wc.hInstance     = hInstance;              
  41.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  42.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  43.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  44.    wc.lpszMenuName  = lpszAppName;              
  45.    wc.lpszClassName = lpszAppName;              
  46.  
  47.    if ( IS_WIN95 )
  48.    {
  49.       if ( !RegisterWin95( &wc ) )
  50.          return( FALSE );
  51.    }
  52.    else if ( !RegisterClass( &wc ) )
  53.       return( FALSE );
  54.  
  55.    hInst = hInstance; 
  56.  
  57.    // Create the main application window.
  58.    //....................................
  59.    hWnd = CreateWindow( lpszAppName, 
  60.                         lpszTitle,    
  61.                         WS_OVERLAPPEDWINDOW, 
  62.                         CW_USEDEFAULT, 0, 
  63.                         CW_USEDEFAULT, 0,  
  64.                         NULL,              
  65.                         NULL,              
  66.                         hInstance,         
  67.                         NULL               
  68.                       );
  69.  
  70.    if ( !hWnd ) 
  71.       return( FALSE );
  72.  
  73.    ShowWindow( hWnd, nCmdShow ); 
  74.    UpdateWindow( hWnd );         
  75.  
  76.    while( GetMessage( &msg, NULL, 0, 0) )   
  77.    {
  78.       if ( hDlgModeless || !IsDialogMessage( hDlgModeless, &msg ) )
  79.       {
  80.          TranslateMessage( &msg ); 
  81.          DispatchMessage( &msg );  
  82.       }
  83.    }
  84.  
  85.    return( msg.wParam ); 
  86. }
  87.  
  88.  
  89. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  90. {
  91.    WNDCLASSEX wcex;
  92.  
  93.    wcex.style         = lpwc->style;
  94.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  95.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  96.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  97.    wcex.hInstance     = lpwc->hInstance;
  98.    wcex.hIcon         = lpwc->hIcon;
  99.    wcex.hCursor       = lpwc->hCursor;
  100.    wcex.hbrBackground = lpwc->hbrBackground;
  101.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  102.    wcex.lpszClassName = lpwc->lpszClassName;
  103.  
  104.    // Added elements for Windows 95.
  105.    //...............................
  106.    wcex.cbSize = sizeof(WNDCLASSEX);
  107.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  108.                             IMAGE_ICON, 16, 16,
  109.                             LR_DEFAULTCOLOR );
  110.             
  111.    return RegisterClassEx( &wcex );
  112. }
  113.  
  114.  
  115. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  116. {
  117.    switch( uMsg )
  118.    {
  119.       case WM_COMMAND :
  120.               switch( LOWORD( wParam ) )
  121.               {
  122.                  case IDM_TEST :
  123.                         if ( !hDlgModeless )
  124.                            hDlgModeless = CreateDialog( hInst, "TestDialog", 
  125.                                                 hWnd, (DLGPROC)TestDlgProc );
  126.                         break;
  127.  
  128.                  case IDM_ABOUT :
  129.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  130.                         break;
  131.  
  132.                  case IDM_EXIT :
  133.                         DestroyWindow( hWnd );
  134.                         break;
  135.               }
  136.               break;
  137.       
  138.       case WM_DESTROY :
  139.               PostQuitMessage(0);
  140.               break;
  141.  
  142.       default :
  143.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  144.    }
  145.  
  146.    return( 0L );
  147. }
  148.  
  149.  
  150. LRESULT CALLBACK TestDlgProc( HWND hDlg, UINT uMsg, 
  151.                               WPARAM wParam, LPARAM lParam )
  152. {
  153.    switch( uMsg )
  154.    {
  155.       // Initially set the check box
  156.       // and radio button states.
  157.       //............................
  158.       case WM_INITDIALOG :
  159.             CheckDlgButton( hDlg, IDC_CHECKBOX, bChecked ? MF_CHECKED :
  160.                                                            MF_UNCHECKED );
  161.             CheckRadioButton( hDlg, IDC_RADIO1, IDC_RADIO2, 
  162.                            bRadio1 ? IDC_RADIO1 : IDC_RADIO2 );
  163.             break;
  164.  
  165.       case WM_COMMAND :
  166.             switch( LOWORD( wParam ) )
  167.             {
  168.                case IDC_CHECKBOX :
  169.                       bChecked = !IsDlgButtonChecked( hDlg, IDC_CHECKBOX );
  170.                       CheckDlgButton( hDlg, IDC_CHECKBOX, 
  171.                                       bChecked ? MF_CHECKED : MF_UNCHECKED );
  172.                       break;
  173.  
  174.                case IDC_RADIO1 :
  175.                       bRadio1 = TRUE;
  176.                       CheckRadioButton( hDlg, IDC_RADIO1, IDC_RADIO2,
  177.                                                           IDC_RADIO1 );
  178.                       break;
  179.  
  180.                case IDC_RADIO2 :
  181.                       bRadio1 = FALSE;
  182.                       CheckRadioButton( hDlg, IDC_RADIO1, IDC_RADIO2,
  183.                                                           IDC_RADIO2 );
  184.                       break;
  185.  
  186.                case IDCANCEL : 
  187.                       DestroyWindow( hDlg ); 
  188.                       break;
  189.             }
  190.             break;
  191.  
  192.        case WM_DESTROY :
  193.             hDlgModeless = NULL;
  194.             break;
  195.  
  196.       default :
  197.             return( FALSE );
  198.    }
  199.  
  200.    return( TRUE );
  201. }
  202.  
  203.  
  204. LRESULT CALLBACK About( HWND hDlg,           
  205.                         UINT message,        
  206.                         WPARAM wParam,       
  207.                         LPARAM lParam)
  208. {
  209.    switch (message) 
  210.    {
  211.        case WM_INITDIALOG: 
  212.                return (TRUE);
  213.  
  214.        case WM_COMMAND:                              
  215.                if (   LOWORD(wParam) == IDOK         
  216.                    || LOWORD(wParam) == IDCANCEL)    
  217.                {
  218.                        EndDialog(hDlg, TRUE);        
  219.                        return (TRUE);
  220.                }
  221.                break;
  222.    }
  223.  
  224.    return (FALSE); 
  225. }
  226.